<?php 
require_once '../functions.php';
xiu_get_current_user();
$status = array(
  'approved' => '批准', 
  'held' => '保留',
  'rejected' => '拒绝'
  );
$page=isset($_GET['page'])?$_GET['page']:1;
//计算分页
$count=(int)xiu_fetch_all('select 
  count(1) as count 
  from comments
  inner join posts on comments.post_id=posts.id
  ')[0]['count'];
$size=5;
$total_page=(int)ceil($count/$size);
$offset=($page-1)*$size;
$visable=5;
$regin=($visable-1)/2;
$begin=$page-$regin;
$end=$begin+$visable;
if($begin<1){
  $begin=1;
  $end=$begin+$visable;
}
if($end>$total_page+1){
  $end=$total_page+1;
  $begin=$end-$visable;
  if($begin<1){
    $begin=1;
  }

}
if($page<1){
  header('Location:comments.php?page=1');
}
if($page>$total_page){
    header("Location:posts.php?page={$total_page}");
}
$comment=xiu_fetch_all("select 
comments.id,
comments.author,
comments.content,
posts.title as posts_title,
comments.created,
comments.status
from comments
inner join posts on comments.post_id=posts.id
order by comments.created desc
limit {$offset},{$size};
") ;
 ?> 
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Comments &laquo; Admin</title>
  <link rel="stylesheet" href="../static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="../static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="../static/assets/css/admin.css">
  <script src="../static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">
    <?php include 'inc/navbar.php'; ?>

    <div class="container-fluid">
      <div class="page-title">
        <h1>所有评论</h1>
      </div>
      <!-- 有错误信息时展示 -->
     <?php if (isset($message)): ?>
        <div class="alert alert-danger">
        <strong>错误！</strong><?php echo $message; ?>
      </div>
     <?php endif ?>
      <div class="page-action">
        <!-- show when multiple checked -->
        <div class="btn-batch" style="display: none" id="btn_batch">
          <a class="btn btn-info btn-sm" id="btn1" href="comments-allow.php">批量批准</a>
          <a class="btn btn-warning btn-sm" id="btn2" href="comments-reject.php">批量拒绝</a>
          <a class="btn btn-danger btn-sm" id="btn3" href="comments-delete.php">批量删除</a>
        </div>
        <ul class="pagination pagination-sm pull-right">
          <li><a href="#">上一页</a></li>
          <?php for($i=$begin;$i<$end;$i++): ?>
            <li<?php echo $i == $page ? ' class="active"' : ''; ?>><a href="?page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
          <?php endfor ?>
          <li><a href="#">下一页</a></li>
        </ul>
      </div>
      <table class="table table-striped table-bordered table-hover">
        <thead>
          <tr>
            <th class="text-center" width="40"><input type="checkbox"></th>
            <th>作者</th>
            <th>评论</th>
            <th>评论在</th>
            <th>提交于</th>
            <th>状态</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($comment as $item): ?>
            <tr class="danger">
              <td class="text-center"><input type="checkbox" data-id="<?php echo $item['id']; ?>"></td>
              <td><?php echo $item['author']; ?></td>
              <td><?php echo $item['content']; ?></td>
              <td><?php echo $item['posts_title']; ?></td>
              <td><?php echo $item['created']; ?></td>
              <td><?php echo $status[$item['status']]; ?></td>
              <td class="text-center">
                <a href="comments-allow.php?id=<?php echo $item['id'].'&page='.$page; ?>" class="btn btn-info btn-xs">批准</a>
                <a href="comments-delete.php?id=<?php echo $item['id'].'&page='.$page; ?>" class="btn btn-danger btn-xs">删除</a>
              </td>
           </tr>
          <?php endforeach ?>                
        </tbody>
      </table>
    </div>
  </div>

  <?php $current_page = 'comments'; ?>
  <?php include 'inc/sidebar.php'; ?>

  <script src="../static/assets/vendors/jquery/jquery.js"></script>
  <script src="../static/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script>
  $(function($){
    var $btn_batch=$("#btn_batch");
    var $btn1=$("#btn1");
    var $btn2=$("#btn2");
    var $btn3=$("#btn3");
    var $input=$("tbody input");
    var $input1=$("thead input");
    var arrAll=[];
    var length=$input.length;
    var flag=false;
    $input.on('change',function(){
      //先获取点击的这个元素的id
      var id=$(this).attr("data-id");
      //判断这个元素是否被够勾中
      if($(this).prop("checked")){
        //判断这个元素在没在数组里
        arrAll.includes(id) || arrAll.push(id);
      }else{
        //如果这个元素没被勾中
        //先摘到这个id在数组里面的位置
        var id1=arrAll.indexOf(id);
        arrAll.splice(id1, 1);
      }
      arrAll.length?$btn_batch.fadeIn():$btn_batch.fadeOut();
      $btn3.prop('search','?id='+arrAll+'&page='+<?php echo $page ?>);
      $btn2.prop('search','?id='+arrAll+'&page='+<?php echo $page ?>);
      $btn1.prop('search','?id='+arrAll+'&page='+<?php echo $page ?>);
      if(arrAll.length==length){
          $input1.prop("checked",true);
      }else{
        $input1.prop("checked",false);
      }
    });
    $input1.on('change',function(){
      flag=$(this).prop("checked");
      $input.prop("checked",flag).change();
    });
  });




  </script>
  <script>NProgress.done()</script>
</body>
</html>
